package com.cloud.animationtestdemo

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.Gravity
import androidx.activity.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.cloud.animationtestdemo.databinding.ActivityMainBinding
import com.cloud.animationtestdemo.strage.BezierCurveAnimationStrategy
import com.cloud.animationtestdemo.strage.QuadraticBezierBaseAnimationStrategy
import com.cloud.animationtestdemo.utils.Constants.animateAlphaWithStrategy
import com.cloud.animationtestdemo.widget.BigImageView
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class MainActivity :  ComponentActivity(){
    private lateinit var binding : ActivityMainBinding
    private var isDarkTheme = true
    val imageUri = Uri.parse("https://img-blog.csdnimg.cn/b286b49591dc4bbd8578300a6e6cfb8e.gif#pic_center")
    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(if (isDarkTheme) R.style.Base_Theme_AnimationTestDemo_Night else R.style.Base_Theme_AnimationTestDemo_Day)
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // initView()
        BigImageView(this@MainActivity).apply {
            loadImage(imageUri)
        }
    }


    private fun initView() {
        //startBezierAnim()
        startScaleAnim()
        binding.tvAnim.setOnClickListener {
            isDarkTheme = !isDarkTheme
            recreate() // 用新的主题重建活动
        }
        binding.tvAnim.gravity = Gravity.CENTER
        Log.d("AnimationStrategy","是否显示${binding.tvAnim.gravity}")
        binding.tvAnim.gravity = Gravity.CENTER
        Log.d("AnimationStrategy","是否显示${binding.tvAnim.gravity}")
    }

    @SuppressLint("Recycle")
    private fun startScaleAnim() {
        lifecycleScope.launch {
            // 位移动画示例
/*            val translateStrategy = TranslateAnimationStrategy()
            translateStrategy.animateView(binding.ivAnim, 1f, 1f, 200f, 200f, 3000L)
            delay(5000L)
            // 缩放动画示例
            val scaleStrategy = ScaleAnimationStrategy()
            scaleStrategy.animateView(binding.ivAnim, 1f, 1f, 2f, 2f, 3000L)
            delay(5000L)*/
            // 透明度动画示例
/*            val alphaStrategy = AlphaAnimationStrategy()
            alphaStrategy.animateView(binding.ivAnim, 0.8f, 1f, 3000L)
            delay(5000L)
            // 转场动画示例（假设进入场景）
            val transitionStrategy = TransitionAnimationStrategy()
            transitionStrategy.animateTransition(binding.ivAnim, true, 3000L)
            delay(5000L)
            // 旋转动画示例
            val rotateStrategy = RotateAnimationStrategy()
            rotateStrategy.animateView(binding.ivAnim, 0f, 360f, binding.ivAnim.width / 2f, binding.ivAnim.height / 2f, 4000L)
            delay(5000L)
            // 基于估值器的颜色渐变动画示例
            val evaluatorStrategy = EvaluatorAnimationStrategy()
            evaluatorStrategy.animateView(binding.ivAnim, Color.RED, Color.BLUE, 4000L)
            delay(5000L)
            // 基于插值器的先快后慢位移动画示例
            val interpolatorStrategy = InterpolatorAnimationStrategy()
            interpolatorStrategy.animateView(binding.ivAnim, 0f, 0f, 200f, 200f, 4000L)
            delay(1000)*/
            // 创建贝塞尔曲线路径
            delay(2000)
            val strategy2 = BezierCurveAnimationStrategy()
            strategy2.executeAnimation(binding.ivAnim, 8000)
        }
    }

    private fun startBezierAnim() {
        // 使用二次贝塞尔曲线策略来实现透明度动画，传入控制点坐标
        val strategy = QuadraticBezierBaseAnimationStrategy(0.4f, 0.74f,1f,0.8f)
        binding.ivAnim.animateAlphaWithStrategy(3000, strategy) // 动画持续3秒
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d("切换主题",newConfig.uiMode.toString())

    }
}